package com.lcc.service.Impl;

import com.lcc.dao.AccountDao;
import com.lcc.dao.BookDao;
import com.lcc.dao.StorehouseDao;
import com.lcc.pojo.*;
import com.lcc.service.BookService;
import com.lcc.utils.MoneyLessException;
import com.lcc.utils.StoreHouseLessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * /**
 *
 * @author 廖聪聪
 * @version 1.0
 * 创建时间 2020/7/23 10:58
 */
@Service
public class BookServiceImpl implements BookService {

	@Autowired
	private BookDao bookDao;
	@Autowired
	private StorehouseDao storehouseDao;
	@Autowired
	private AccountDao accountDao;

	public int findCount(Query query) {
		return bookDao.findCount(query);
	}

	public Book findByidBook(Integer bid) {
		return bookDao.findByidBook(bid);
	}

	/**
	 * 批量删除书籍
	 * @param ids
	 * @return
	 */
	public boolean deletBookByid(String[] ids) {
		boolean flag = false;
		for (String id:ids){
			flag = bookDao.deletBookByid(new Integer(id));
		}
		return flag;
	}

	public boolean addBook(Book book) {
		return bookDao.addBook(book);
	}

	public boolean addToCart(int id, ShoppingCart sc) {
		Book book = bookDao.findByidBook(id);
		if (book!=null){
			sc.addBook(book);
			return true;
		}
		return false;
	}

	/**
	 * 清空购物车
	 * @param sc
	 */
	public void clearShoppingCart(ShoppingCart sc) {
		sc.clear();
	}

	public void removeItemFromShoppingCart(ShoppingCart sc, int id) {
		sc.removeItem(id);
	}

	public void updateItemQuantity(ShoppingCart sc, int id, int quantity) {
		sc.updateItemQuantity(id,quantity);
	}

	/**
	 * 购买书
	 * @param num
	 * @param bookid
	 * @param accid
	 */
	@Transactional(isolation= Isolation.READ_COMMITTED,propagation= Propagation.REQUIRED,rollbackFor= {StoreHouseLessException.class,MoneyLessException.class})
	public void buybook(Integer num, Integer bookid, Integer accid) {

		Book book= bookDao.findByidBook(bookid);
		int cout=book.getStorehouse().getBook_count();
		if (cout<=0){
			throw new StoreHouseLessException("库存不足了");
		}
        //库存数量减少
		Storehouse storehouse = new Storehouse();

		storehouse.setBookid(bookid);
		storehouse.setBook_count(num);
		//System.out.println("storehouse:"+storehouse);
		storehouseDao.updateStore(storehouse);
		//余额减少
		Taccount taccount = new Taccount();
		taccount.setAccid(accid);
		Taccount bybalance = accountDao.findBybalance(accid);
		//System.out.println("accid------------"+bybalance.getBalance());
		//System.out.println("bookid"+book);
		Double book_price = book.getBook_price();
		taccount.setBalance(num*book_price);
		//System.out.println("taccount:"+taccount);
		if (bybalance.getBalance()<book.getBook_price()){
			throw new MoneyLessException("余额不足了！！！");
		}
		accountDao.updatebance(taccount);
	}

	public List<Book> findByBook(Query query, PageBean pageBean, String pg) {
		pageBean.setTotalsize(findCount(query));
		System.out.println("pd"+pg);
		int puNum = 0;
		if (pg==null){
			//说明用户是从登录页面来
			puNum=1;
		}else {
			puNum = new Integer(pg);
		}
		if (puNum<1){
			puNum=1;
		}
		if (puNum>pageBean.getTotalPage()){
			puNum = pageBean.getTotalPage();
		}
		pageBean.setCp(puNum);
		query.setStart((pageBean.getCp()-1)*pageBean.getPs());
        query.setEnd(pageBean.getPs()*pageBean.getCp());
        pageBean.setQuery(query);
		return bookDao.findByBook(pageBean);

	}
}
